Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

if-else ladder

Else if ladder examples

Here are few examples to understand the concept and syntax of else if ladder.

Example 1: Age-based Discounts

Else if ladder example
public class Main{ public static void main(String[] args) { int age = 38; // Check if age is less than 18 if (age < 18) { System.out.println("Youth discount applies."); } // If age is not less than 18, check if age is between 18 and 30 (inclusive) else if (age >= 18 && age <= 30) { System.out.println("Young adult discount applies."); } // If age is not in the previous ranges, check if age is between 31 and 60 (inclusive) else if (age >= 31 && age <= 60) { System.out.println("Regular adult fare applies."); } // If none of the previous conditions are met, apply senior citizen discount else { System.out.println("Senior citizen discount applies."); } } }

Output

Regular adult fare applies.
In this example, The code begins by declaring an int variable named age and assigning it a value of 28, which represents the age of the person. The code then uses a series of if and else if statements to evaluate the age and determine the applicable discount: The first if statement checks whether the age is less than 18. If this condition is true, it means the person is under 18 years old, and the program prints "Youth discount applies." If the first if condition is not met (i.e., the person is not under 18), the program moves to the else if statement that checks if the age is between 18 and 30 (inclusive). If this condition is true, the program prints "Young adult discount applies." If neither of the first two conditions is met, the program proceeds to the next else if statement, which checks if the age is between 31 and 60 (inclusive). If this condition is true, the program prints "Regular adult fare applies." If none of the previous conditions are met, it means the person is 61 years old or older, and the program prints "Senior citizen discount applies." In summary, the code determines the age-based discount category and prints one of the following messages: If age is less than 18: "Youth discount applies." If age is between 18 and 30: "Young adult discount applies." If age is between 31 and 60: "Regular adult fare applies." If age is 61 or older: "Senior citizen discount applies."

Example 2: Tax Calculation

Tax Calculation Example
public class Main{ public static void main(String[] args) { double income = 50000; double tax = 0; if (income <= 20000) { // The person owes no tax. tax = 0; } else if (income > 20000 && income <= 50000) { // The person owes 10% of their income over 20,000. tax = 0.1 * (income - 20000); } else if (income > 50000 && income <= 100000) { // The person owes 3,000 plus 20% of their income over 50,000. tax = 3000 + 0.2 * (income - 50000); } else { // The person owes 13,000 plus 30% of their income over 100,000. tax = 13000 + 0.3 * (income - 100000); } System.out.println("Tax: $" + tax); } }

Output

Tax: $3000
In this example,The income variable stores the person's income, and the tax variable stores the amount of tax that the person owes. The code then uses a series of if-else statements to determine the amount of tax that the person owes. The first if statement checks if the person's income is less than or equal to 20,000. If it is, then the person owes no tax. Otherwise, the code checks if the person's income is greater than 20,000 and less than or equal to 50,000. If it is, then the person owes 10% of their income over 20,000. Otherwise, the code checks if the person's income is greater than 50,000 and less than or equal to 100,000. If it is, then the person owes 3,000 plus 20% of their income over 50,000. Otherwise, the person owes 13,000 plus 30% of their income over 100,000.

Example 3: BMI Calculation

BMI Calculation
public class Main{ public static void main(String[] args) { double weight = 70; // in kilograms double height = 1.75; // in meters double bmi = weight / (height * height); String category = ""; // Check BMI and assign the appropriate category if (bmi < 18.5) { category = "Underweight"; } else if (bmi >= 18.5 && bmi < 24.9) { category = "Normal weight"; } else if (bmi >= 25 && bmi < 29.9) { category = "Overweight"; } else if (bmi >= 30 && bmi < 34.9) { category = "Obese (Class I)"; } else if (bmi >= 35 && bmi < 39.9) { category = "Obese (Class II)"; } else { category = "Obese (Class III)"; } // Print the BMI category System.out.println("BMI Category: " + category); } }

Output

BMI Category: Normal weight
In this example, The code begins by declaring two double variables: weight (with a value of 70 kilograms) and height (with a value of 1.75 meters). These variables represent the person's weight and height, respectively. The BMI is calculated using the formula weight / (height * height). The code uses a series of if and else if statements to determine the BMI category based on the calculated BMI: The first if statement checks if the BMI is less than 18.5, which is the threshold for being underweight. If this condition is met, the category variable is set to "Underweight". The subsequent else if statements check different ranges of BMI to determine the corresponding category: "Normal weight," "Overweight," "Obese (Class I)," "Obese (Class II)," and "Obese (Class III)". If none of the previous conditions are met, it means the BMI is 40 or higher (which corresponds to the last category), and the category variable is set to "Obese (Class III)". After determining the BMI category, the code prints the result using the System.out.println() statement. In summary, the code calculates the BMI based on weight and height, determines the BMI category, and prints the corresponding category based on various thresholds.

Example 4: Season Finder

Season finder
public class Main{ public static void main(String args[]) { int month = 4; // April String season; if (month == 12 || month == 1 || month == 2) season = "Winter"; else if (month == 3 || month == 4 || month == 5) season = "Spring"; else if (month == 6 || month == 7 || month == 8) season = "Summer"; else if (month == 9 || month == 10 || month == 11) season = "Autumn"; else season = "Bogus Month"; System.out.println("April is in the " + season + "."); } }

Output

April is in the Spring.
In this example, The code defines a class named IfElse with a main method. Inside the main method, an int variable named month is declared and assigned the value 4, representing the month of April. A String variable named season is declared to store the determined season based on the given month. The code then uses a series of if and else if statements to determine the season based on the value of the month variable: The first if statement checks if the month is either December (12), January (1), or February (2). If the condition is true, it assigns the value "Winter" to the season variable. The subsequent else if statements cover different ranges of months to assign the corresponding seasons: "Spring" for March (3), April (4), and May (5); "Summer" for June (6), July (7), and August (8); "Autumn" for September (9), October (10), and November (11). If none of the previous conditions are met, it means the month is not within the range of valid months, so the season is set to "Bogus Month". After determining the season, the code prints the result using the System.out.println() statement. In summary, the code determines the season for the month of April (represented by the value 4) and prints the result: "April is in the Spring."

  📌TAGS

★If else ladder condition ★java ★java if ★if else ★nested if else ★nested if ★conditional statements

Tutorials